﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2013-07-10
//
// Update Date : 
//
// Copyright (c) Fuel Game Engine (http://www.fge-engine.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once

#if FUEL_MODULE_UI
// 注册贴图回调函数
DWORD FCALL FUI_Func_CreateTexFunc(FUI_INIT* uit, const ACHAR* filename, funit& out_texw, funit& out_texh)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    return (DWORD)fge->GetTexture()->Create(filename, true);
}
// 释放贴图回调函数
void  FCALL FUI_Func_DestroyTexFunc(FUI_INIT* uit, DWORD tex_id)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    fge->GetTexture()->Destroy((FIDTEXTURE)tex_id);
}
// 绘制贴图回调函数
void  FCALL FUI_Func_RenderFunc(FUI_INIT* uit, DWORD tex_id, fuRect* dst, fuRect* src, fuSize* pic_size, DWORD color, BYTE sample, BYTE reversal)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    DWORD flags = eRF_Default2D;
    if (sample & eUTS_Liner)
        flags |= eRF_TextureFilter;
    if (sample & eUTS_BlendOne)
        flags |= eRF_DstBlendOne;
    switch (reversal)
    {
    case 1:
        fge->GetRender()->DrawSprite((FIDTEXTURE)tex_id,
            dst->uPos.nX + dst->uSize.nW, dst->uPos.nY, -dst->uSize.nW, dst->uSize.nH,
            src->uPos.nX, src->uPos.nY, src->uSize.nW, src->uSize.nH,
            color, flags|eRF_CullCW);
        break;
    case 2:
        fge->GetRender()->DrawSprite((FIDTEXTURE)tex_id,
            dst->uPos.nX, dst->uPos.nY + dst->uSize.nH, dst->uSize.nW, -dst->uSize.nH,
            src->uPos.nX, src->uPos.nY, src->uSize.nW, src->uSize.nH,
            color, flags|eRF_CullCW);
        break;
    case 3:
        fge->GetRender()->DrawSprite((FIDTEXTURE)tex_id,
            dst->uPos.nX + dst->uSize.nW, dst->uPos.nY + dst->uSize.nH, -dst->uSize.nW, -dst->uSize.nH,
            src->uPos.nX, src->uPos.nY, src->uSize.nW, src->uSize.nH,
            color, flags);
        break;
    default:
        fge->GetRender()->DrawSprite((FIDTEXTURE)tex_id,
            dst->uPos.nX, dst->uPos.nY, dst->uSize.nW, dst->uSize.nH,
            src->uPos.nX, src->uPos.nY, src->uSize.nW, src->uSize.nH,
            color, flags);
    };
}
// 绘制2D线框贴图回调函数
void  FCALL FUI_Func_DrawRectFunc(FUI_INIT* uit, fuRect* dst, DWORD color, bool fill_it)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    FRect rc(dst->uPos.nX, dst->uPos.nY, dst->uSize.nW, dst->uSize.nH);
    fge->GetRender()->DrawRect(&rc, color, fill_it);
}
// 添加裁剪区或者去掉裁剪区
void  FCALL FUI_Func_ClipFunc(FUI_INIT* uit, fuRect* wnd_rc)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    fge->GetRender()->Clip2D((FRect*)wnd_rc);
}
// 创建字体
DWORD FCALL FUI_Func_CreateFntFunc(FUI_INIT* uit, const ACHAR* name, BYTE size, bool bold, bool smooth)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    return (DWORD)fge->GetFont()->Create(name, size, bold, smooth);
}
// 查询字体信息
bool FCALL FUI_Func_QueryFntFunc(FUI_INIT* uit, DWORD fnt, const WCHAR* str, bool queryMaxLine, IFUI_FontQuery* vfq, size_t& count, size_t& fnt_h)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    if (!str || !*str)
        return false;
    if (!vfq)
        return fge->GetFont()->QueryChar(str, (FIDFONT)fnt, count, fnt_h);
    FV_FONT_QUERY vff(count);
    if (!fge->GetFont()->QueryString(str, (FIDFONT)fnt, queryMaxLine, vff.get(), count))
        return false;
    fnt_h = fge->GetFont()->GetFontData((FIDFONT)fnt)->nFontHeight;
    for (size_t i = 0; i < count; ++i)
    {
        vfq[i].nCharCount = vff[i].nCharCount;
        vfq[i].nPixelWidth = vff[i].nPixelWidth;
    }
    return true;
}
// 释放字体
void  FCALL FUI_Func_DestroyFntFunc(FUI_INIT* uit, DWORD fnt_id)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    fge->GetFont()->Destroy((FIDFONT)fnt_id);
}
// 绘制文字
void  FCALL FUI_Func_DrawTextFunc(FUI_INIT* uit, fuRect* dst, const WCHAR *str, DWORD fnt_id, DWORD color, BYTE efc, DWORD efc_color, BYTE flags)
{
    IFEngine* fge = (IFEngine*)uit->userPTR;
    FRect rc(dst->uPos.nX, dst->uPos.nY, dst->uSize.nW, dst->uSize.nH);
    rc.X = (float)FGetRound(rc.X);
    rc.Y = (float)FGetRound(rc.Y);
    rc.W = (float)FGetRound(rc.W);
    rc.H = (float)FGetRound(rc.H);
    DWORD align = eFA_LEFT | eFA_TOP;
    switch (flags)
    {
    case eUAM_LeftTop:      align = eFA_LEFT   | eFA_TOP;     break;
    case eUAM_RightTop:     align = eFA_RIGHT  | eFA_TOP;     break;
    case eUAM_LeftBottom:   align = eFA_LEFT   | eFA_BOTTOM;  break;
    case eUAM_RightBottom:  align = eFA_RIGHT  | eFA_BOTTOM;  break;
    case eUAM_Middle:       align = eFA_CENTER | eFA_VCENTER; break;
    case eUAM_TopMiddle:    align = eFA_CENTER | eFA_TOP;     break;
    case eUAM_BottomMiddle: align = eFA_CENTER | eFA_BOTTOM;  break;
    case eUAM_LeftMiddle:   align = eFA_LEFT   | eFA_VCENTER; break;
    case eUAM_RightMiddle:  align = eFA_RIGHT  | eFA_VCENTER; break;
    default: assert(false && "错误的对齐模式");
    }
    EF_FONT_EFFECT effect = eFE_Normal;
    switch (efc)
    {
    case eUTET_Normal: effect = eFE_Normal; break;
    case eUTET_Shadow: effect = eFE_Shadow; break;
    case eUTET_Fringe: effect = eFE_Fringe; break;
    }
    fge->GetFont()->DrawText(&rc, str, (FIDFONT)fnt_id, color, align, effect, efc_color);
}
// 加载文件到内存
bool  FCALL FUI_Func_LoadFileFunc(FUI_INIT* uit, const ACHAR* file, FV_UI_BYTE& out)
{
    FV_BYTE buf;
    if (FFileReadA(file, &buf))
    {
        out.resize(buf.size());
        FMemcpy(&out[0], &buf[0], buf.size());
        return true;
    }
    return false;
}
FINLINE void FUI_InitFuncInterface(FUI_INIT& it)
{
    it.create_tex   = FUI_Func_CreateTexFunc;
    it.destroy_tex  = FUI_Func_DestroyTexFunc;
    it.render_spr   = FUI_Func_RenderFunc;
    it.drawrect     = FUI_Func_DrawRectFunc;
    it.cliprect     = FUI_Func_ClipFunc;
    it.create_fnt   = FUI_Func_CreateFntFunc;
    it.query_fnt    = FUI_Func_QueryFntFunc;
    it.destroy_fnt  = FUI_Func_DestroyFntFunc;
    it.draw_text    = FUI_Func_DrawTextFunc;
    it.load_file    = FUI_Func_LoadFileFunc;
};
#endif